#!/bin/bash
# To run this script you'll need to download the ultra-high res
# scan of Starry Night from the Google Art Project, using this command:
# wget -c https://upload.wikimedia.org/wikipedia/commons/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg -O starry_night_gigapixel.jpg
# Or you can manually download the image from here: https://commons.wikimedia.org/wiki/File:Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg
source ~/.bashrc

if [ ! -n "$1" ] ;then
    echo "you have not input a agrs!"
    exit;
fi
cd /home/renkeju/cuda/neural-style-pt
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
echo "$startTime"

SERVER=43.228.182.78
STYLE_IMAGE=/home/renkeju/projects/aiserver/uploads/high/$2
CONTENT_IMAGE=/home/renkeju/projects/aiserver/uploads/images/$3
OUTPUT=/home/renkeju/projects/aiserver/uploads/output
ID=$1
wget "http://${SERVER}:3000/images/$3" -O $CONTENT_IMAGE

STYLE_WEIGHT=5e2
STYLE_SCALE=1.0

STYLE_WEIGHT2=2500 # Style weight for image size 2048 and above

PYTHON=python3 # Change to Python if using Python 2
SCRIPT=neural_style.py
GPU="0"

NEURAL_STYLE=$PYTHON
NEURAL_STYLE+=" "
NEURAL_STYLE+=$SCRIPT

# Uncomment if using pip package
#NEURAL_STYLE=neural-style

curl --location --request PUT "http://$SERVER:3000/task" \
--form 'id="'${ID}'"' \
--form 'progress="10"' | jq '.data'

$NEURAL_STYLE \
  -content_image $CONTENT_IMAGE \
  -style_image $STYLE_IMAGE \
  -style_scale $STYLE_SCALE \
  -print_iter 1 \
  -style_weight $STYLE_WEIGHT \
  -image_size 256 \
  -output_image output/${ID}1.png \
  -tv_weight 0 \
  -gpu $GPU \
  -backend cudnn -cudnn_autotune 1>/dev/null

curl --location --request PUT "http://$SERVER:3000/task" \
--form 'id="'${ID}'"' \
--form 'progress="20"' | jq '.data'

$NEURAL_STYLE \
  -content_image $CONTENT_IMAGE \
  -style_image $STYLE_IMAGE \
  -init image -init_image output/${ID}1.png \
  -style_scale $STYLE_SCALE \
  -print_iter 1 \
  -style_weight $STYLE_WEIGHT \
  -image_size 512 \
  -num_iterations 500 \
  -output_image output/${ID}2.png \
  -tv_weight 0 \
  -gpu $GPU \
  -backend cudnn -cudnn_autotune 1>/dev/null

curl --location --request PUT "http://$SERVER:3000/task" \
--form 'id="'${ID}'"' \
--form 'progress="40"' | jq '.data'

$NEURAL_STYLE \
  -content_image $CONTENT_IMAGE \
  -style_image $STYLE_IMAGE \
  -init image -init_image output/${ID}2.png \
  -style_scale $STYLE_SCALE \
  -print_iter 1 \
  -style_weight $STYLE_WEIGHT \
  -image_size 1024 \
  -num_iterations 200 \
  -output_image output/${ID}3.png \
  -tv_weight 0 \
  -gpu $GPU \
  -backend cudnn -cudnn_autotune 1>/dev/null

curl --location --request PUT "http://$SERVER:3000/task" \
--form 'id="'${ID}'"' \
--form 'progress="70"' | jq '.data'

$NEURAL_STYLE \
  -content_image $CONTENT_IMAGE \
  -style_image $STYLE_IMAGE \
  -init image -init_image output/${ID}3.png \
  -style_scale $STYLE_SCALE \
  -print_iter 1 \
  -style_weight $STYLE_WEIGHT2 \
  -image_size 1536 \
  -num_iterations 200 \
  -output_image output/${ID}4.png \
  -tv_weight 0 \
  -gpu $GPU \
  -backend cudnn -cudnn_autotune -optimizer adam 1>/dev/null

# $NEURAL_STYLE \
#   -content_image $CONTENT_IMAGE \
#   -style_image $STYLE_IMAGE \
#   -init image -init_image output/${ID}3.png \
#   -style_scale $STYLE_SCALE \
#   -print_iter 1 \
#   -style_weight $STYLE_WEIGHT2 \
#   -image_size 2048 \
#   -num_iterations 200 \
#   -output_image output/${ID}4.png \
#   -tv_weight 0 \
#   -gpu c,$GPU \
#   -multidevice_strategy 3 \
#   -backend cudnn -cudnn_autotune -optimizer adam

#$NEURAL_STYLE \
#  -content_image $CONTENT_IMAGE \
#  -style_image $STYLE_IMAGE \
#  -init image -init_image ${ID}4.png \
#  -style_scale $STYLE_SCALE \
#  -print_iter 1 \
#  -style_weight $STYLE_WEIGHT2 \
#  -image_size 2350 \
#  -num_iterations 200 \
#  -output_image ${ID}5.png \
#  -tv_weight 0 \
#  -gpu c,$GPU \
#  -multidevice_strategy 5 \
#  -backend cudnn -cudnn_autotune -optimizer adam


endTime=`date +%Y%m%d-%H%M%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo "Make complete. $startTime --> $endTime" "Total:$sumTime seconds"

mv output/${ID}4.png finished/${ID}-${endTime}.png
# cp finished/${ID}-${endTime}.png $OUTPUT
convert finished/${ID}-${endTime}.png -resize 260x260 outputs/${ID}-${endTime}.png
# convert finished/${ID}-${endTime}.png -resize 260x260 ${OUTPUT}s/${ID}-${endTime}.png

./upload.sh output finished ${ID}-${endTime}.png
./upload.sh outputs outputs ${ID}-${endTime}.png

curl --location --request PUT "http://$SERVER:3000/task" \
--form 'id="'${ID}'"' \
--form 'product="'${ID}'-'${endTime}'.png"' \
--form 'progress="100"' | jq '.data'

rm -Rf output/${ID}*
echo "Upload to complete."